//
//  MBSpaceSetViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/26.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceSetViewController.h"
#import "MBSpaceSetTimerViewController.h"
#import "MBSpaceAboutViewController.h"
#import "MBLoginController.h"

#import "MBSpaceSetCell.h"
#import "AttentionView.h"

#import "WXApi.h"
#import <TencentOpenAPI/TencentOAuth.h>
#define MIBAOTU_GROUD (@"15235485")

@interface MBSpaceSetViewController ()<UITableViewDataSource,UITableViewDelegate,MBSpaceSetCellDelegate,AttentionViewDelegate> {
    UITableView             *_tableView;
    NSMutableArray          *_mutArr;
    int                     selectIndexForPath;
    CLShareManager                  *shareView;          //分享

}

@end

@implementation MBSpaceSetViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _tableView.tableFooterView = [self addTableFootView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    selectIndexForPath = 0;
    // Do any additional setup after loading the view.
    [self setTitleView:@"设置"];
    [self setBackItem];
    
    [self onCreateUI];
    
    [self loadData];
}

- (void)loadData
{
    _mutArr = [[NSMutableArray alloc]initWithObjects:@{@"pic":@"zuijinbofang_kj",@"text":@"定时关闭"},@{@"pic":@"qingli_sz",@"text":@"清理缓存"},@{@"pic":@"yunxiazai_sz",@"text":@"收藏夹歌曲自动下载"},@{@"pic":@"qq_sz",@"text":@"加入米宝兔妈妈群"},@{@"pic":@"fenxiang_sz",@"text":@"分享米宝兔"},@{@"pic":@"guanyu_sz",@"text":@"关于米宝兔"}, nil];
    [_tableView reloadData];
}

#pragma mark - UI
- (void)onCreateUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.backgroundView = nil;
    [self.view addSubview:_tableView];
    _tableView .tableHeaderView = [self addTableHeadView];
    _tableView.tableFooterView = [self addTableFootView];
    //分享
    shareView = [[CLShareManager alloc] init];
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.view.height);
    _tableView .tableHeaderView = [self addTableHeadView];
    _tableView.tableFooterView = [self addTableFootView];

    [_tableView reloadData];
}

- (UIView *)addTableHeadView
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20*PERCEN)];
    bgView.backgroundColor = [UIColor clearColor];
    return bgView;
}

- (UIView *)addTableFootView
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200*PERCEN)];
    bgView.backgroundColor = [UIColor clearColor];
    
    UIButton *_loginBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _loginBt.frame = CGRectMake(0, 80*PERCEN, bgView.width, 80*PERCEN);
    if (ISIPAD) {
        _loginBt.frame = CGRectMake((bgView.width-700*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.))/2., 50*PERCEN, 700*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.), 100*PERCEN);
        _loginBt.layer.cornerRadius = 5.f;
        _loginBt.layer.masksToBounds = YES;
    }
    _loginBt.backgroundColor = TINK_COLOR;
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model) {
        [_loginBt setTitle:@"登录帐号" forState:UIControlStateNormal];
        [_loginBt setTitle:@"登录帐号" forState:UIControlStateHighlighted];
    } else {
        [_loginBt setTitle:@"退出当前帐号" forState:UIControlStateNormal];
        [_loginBt setTitle:@"退出当前帐号" forState:UIControlStateHighlighted];

    }
    [_loginBt setTitleColor:UIColorFromHex(0xffffff) forState:UIControlStateNormal];
    [_loginBt setTitleColor:UIColorFromHex(0xffffff) forState:UIControlStateHighlighted];
    _loginBt.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [_loginBt addTarget:self action:@selector(onLogOutAction) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:_loginBt];

    return bgView;
}

#pragma mark - 推出帐号
- (void)onLogOutAction
{
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model) {
        MBLoginController *registerLastVC = [[MBLoginController alloc] init];
       registerLastVC.formBaseViewCtrl = NSStringFromClass([self class]);
             [self.navigationController pushViewController:registerLastVC animated:YES];
       
       
    } else {
        [[UserManager standerDefult] setUserModel:nil];
        _tableView.tableFooterView = nil;
        [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:@"login_answer"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        [self back];
    }
   
}

#pragma mark - 是否自动下载
- (void)onSelectSwitchONForDownMusic:(BOOL)isON withView:(MBSpaceSetCell *)view
{
    [UserManager standerDefult].isLoadMusic = isON;
    
    [[NSUserDefaults standardUserDefaults]setObject:(isON?@"ok":@"") forKey:@"isLoadMusic"];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

#pragma mark - 弹出框 取消、确认
- (void)onSelectSureItem:(AttentionView *)view
{
    
}
- (void)onSelectCancelItem:(AttentionView *)view
{
    //ok
    if (selectIndexForPath == 1) {
        [[UserManager standerDefult]clearCache];
    }
    else if (selectIndexForPath == 3) {
        
        if ([TencentOAuth iphoneQQInstalled]) {
            
            [self joinGroup:@"122858073" key:@"zWPV18qEn201XtKDyc1qegbWVPEdZXXK"];
           //            //已安装
//            NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"weixin://%@",K_WX_AppID]];
//            [[UIApplication sharedApplication] openURL:url];
        } else {
            [ErrorMessage showErrorMessage:@"已复制群号,请打开QQ客户端" inView:self.view];
            UIPasteboard *pboard = [UIPasteboard generalPasteboard];
            pboard.string = MIBAOTU_GROUD;
        }
        
    }
}
- (BOOL)joinGroup:(NSString *)groupUin key:(NSString *)key{
    NSString *urlStr = [NSString stringWithFormat:@"mqqapi://card/show_pslcard?src_type=internal&version=1&uin=%@&key=%@&card_type=group&source=external", groupUin,key];
    NSURL *url = [NSURL URLWithString:urlStr];
    if([[UIApplication sharedApplication] canOpenURL:url]){
        [[UIApplication sharedApplication] openURL:url];
        return YES;
    }
    else return NO;
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _mutArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //歌曲
    static NSString *cellID = @"space_set_Cell";
    MBSpaceSetCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[MBSpaceSetCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.delegate = self;
    }
    if (_mutArr.count > indexPath.row) {
        cell.dataDic = [_mutArr objectAtIndex:indexPath.row];
    }
    
    cell.orientation = self.orientation;
    if (indexPath.row == 2) {
        cell.isShowSwitch = YES;
        cell.isShowSwitchOn = [UserManager standerDefult].isLoadMusic;
    } else {
        cell.isShowSwitch = NO;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100*PERCEN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        MBSpaceSetTimerViewController *show = [[MBSpaceSetTimerViewController alloc]init];
        [self.navigationController pushViewController:show animated:YES];
    }
    else if (indexPath.row == 1) {
        //缓存
        selectIndexForPath = 1;
        float size = [[UserManager standerDefult]folderSizeAtPath];
        AttentionView *_attention = [[AttentionView alloc]initWithTitle:[NSString stringWithFormat:@"共%.2fM缓存，是否确认清除?",size] withCancelBt:@"确认" withSureBt:@"取消"];
        _attention.delegate = self;
        _attention.orientation = self.orientation;
        [_attention showInWindow];
        
    }
    else if (indexPath.row == 2) {
        //自动
    }
    else if (indexPath.row == 3) {
        //加入
        selectIndexForPath = 3;
        NSString *phone = MIBAOTU_GROUD;
        AttentionView *_attention = [[AttentionView alloc]initWithTitle:[NSString stringWithFormat:@"米宝兔妈妈群: %@已复制。赶快去加群吧!",phone] withCancelBt:@"马上去" withSureBt:@"取消"];
        _attention.delegate = self;
         _attention.orientation = self.orientation;
        [_attention showInWindow];
    }
    else if (indexPath.row == 4) {
        //分享
        NSLog(@"分享");
        [shareView setShareVC:self content:[NSString stringWithFormat:@"分享:%@",@"亲子秀唱儿歌"] image:[UIImage imageNamed:@"logo_dl_29"] url:[NSString stringWithFormat:@"%@",@"https://itunes.apple.com/app/id1175970585"]];
        [shareView show];
    }
    else if (indexPath.row == 5) {
        //关于
        MBSpaceAboutViewController *show = [[MBSpaceAboutViewController alloc]init];
        [self.navigationController pushViewController:show animated:YES];
    }
    
}


@end
